<?php

namespace App\Http\Controllers\Car;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\Car\CarUsed;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CarUsedController extends Controller
{
    use ResponseData;
    public function index(CarUsed $model,Request $request) {
        $size = $request->input('size',10);
        if($request->filled('status')){
            $model = $model->where('status',$request->status);
        }
        $model = $model->whereNull('deleted_at');
        $data = $model->orderBy('sort','desc')->paginate($size);
        return $this->success($data);
    }
    public function store(CarUsed $model,Request $request){
        $data = $request->all();
        // $data['existing_sn'] = $this->getSn();
        $model->fill($data);
        $model->save();
        return $this->success('保存成功');
    }
    //设置订单号
    private function getSn(){
        $car = CarUsed::query()
            ->select('id')
            ->where('id','>',0)
            ->orderByDesc('id')
            ->first();
        if($car){
            $num = $car->id+1;
        }else{
            $num = 1;
        }
        return 'cz'.date('Ymd',time()).$num;
    }
    public function edit(CarUsed $model,Request $request){
        $data = $model->find($request->id);
        return $this->success($data);
    }
    public function update(CarUsed $model,Request $request){
        $data = $model->find($request->id);
        $data->update($request->all());
        return $this->success('修改成功');
    }
    public function destory(CarUsed $model,Request $request){
        $data = $model->find($request->id);
        $data->deleted_at = now();
        $data->save();
        return $this->success('删除成功');
    }
}
